<?php
namespace app\dsadmin\controller;
use app\dsadmin\model\OrderModel;
use think\Db;
use think\Cache;

class Order extends Base
{
    public function index()
    {
        $key = input('key');
        $riqi = input('riqi');
        $wxddh = trim(input('wxddh'));
        $map = [];
        $map['order_type'] = ['<>',3];
        if ($key)
        {
            $map['userid'] = $key;
        }
        if ($riqi)
        {
            $map['shijian'] = ['like', "%" . $riqi . "%"];
        }
        
        if ($wxddh)
        {
            $map['wxddh'] = $wxddh;
        }
        
        $order = new OrderModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 15; // 获取总条数
        $count = $order->getAllCount($map); //计算总页面
        $allpage = intval(ceil($count / $limits));

        $lists = $order->getOrderByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('riqi', $riqi);
        if (input('get.page'))
        {
            foreach ($lists as &$v){
                $dwz = $ddh2 = substr($v['ddh'], 0, 10);
                $v['dwz'] = Db::name('dwz')->where(['ddh' => $dwz])->value('dwz');
            }
            unset($v);
            return json($lists);
        }
        $map = [];
        if (!empty($key))
        {
            $map['userid'] = $key;
        }
        if (!empty($riqi))
        {
            $day = $riqi;
            $zuori = date("Y-m-d", strtotime("$day-1 day"));

            $this->assign('day', $day);
            $this->assign('zuori', $zuori);
        }
        else
        {
            $day = date("Y-m-d");
            $zuori = date("Y-m-d", strtotime("$day-1 day"));
        }
        
        
           
           
        
        
        $map['shijian'] = ['like', "%" . $day . "%"];
        $daydd = Db::name("dingdan")->where($map)->where(['order_type'=>['<>',3]])->count();
        $this->assign('daydd', $daydd);

        $money1 = Db::name("dingdan")->where($map)->where(['order_type'=>['<>',3]])->sum('money');
        $this->assign('money1', round($money1, 2));
        
        
       
        $map['shijian'] = ['like', "%" . $zuori . "%"];
        $zdd = Db::name("dingdan")->where($map)->where(['order_type'=>['<>',3]])->count();
        $this->assign('zdd', $zdd);

        $money2 = Db::name("dingdan")->where($map)->where(['order_type'=>['<>',3]])->sum('money');
        $this->assign('money2', round($money2, 2));
        
        
         $daytoji    = date("Y-m-d");
         $tongjitime = strtotime(date("Y-m-d", strtotime("$daytoji-1 day")));
        
        //  $res = Db::name('fangwen')->where(['time' => $tongjitime])->find();
        
        //  if($res['money'] <= 0){
             
        //      Db::name('fangwen')->where(['time' => $tongjitime])->update(['money' => $money2]);
             
        //  }

        $map = [];
        if (!empty($key))
        {
            $map['userid'] = $key;
        }
        $zong = Db::name("dingdan")->where($map)->count();
        $this->assign('zong', $zong);
        
        $money3  = Db::name("dingdan")->where($map)->sum('money');
         
        //今日巅峰
        $mapydf['shijian'] = ['like', "%" . $day . "%"];
        $mapydf['type'] = "dianfeng_pay";
        $moneydf  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($mapydf)->sum('money');
        $this->assign('moneydf', round($moneydf,2));
        
        //今日618
        $mapy618['shijian'] = ['like', "%" . $day . "%"];
        $mapy618['type'] = "618pay";
        $money618  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($mapy618)->sum('money');
        $this->assign('money618', round($money618,2));
        
        //今日蘑菇
        $maplymg['shijian'] = ['like', "%" . $day . "%"];
        $maplymg['type'] = "mogupay";
        $moneymg  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplymg)->sum('money');
        $this->assign('moneymg', round($moneymg,2));
        
        //今日冈本
        $maplygbjd['shijian'] = ['like', "%" . $day . "%"];
        $maplygbjd['type'] = "gangen_jindie_pay";
        $moneygbjd  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplygbjd)->sum('money');
        $this->assign('moneygbjd', round($moneygbjd,2));
        
        //今日冈本金蝶
        $maplygb['shijian'] = ['like', "%" . $day . "%"];
        $maplygb['type'] = "gangben_pay";
        $moneygb  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplygb)->sum('money');
        $this->assign('moneygb', round($moneygb,2));
        
        //今日鸡sir
        $maplyjs['shijian'] = ['like', "%" . $day . "%"];
        $maplyjs['type'] = "jisirwxpay";
        $moneyjs  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplyjs)->sum('money');
        $this->assign('moneyjs', round($moneyjs,2));
        
       
        //昨日鸡sir
        $maplyjsz['shijian'] = ['like', "%" . $zuori . "%"];
        $maplyjsz['type'] = "jisirwxpay";
        $moneyjsz  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplyjsz)->sum('money');
        $this->assign('moneyjsz', round($moneyjsz,2));
        //昨日巅峰
        $maplydfz['shijian'] = ['like', "%" . $zuori . "%"];
        $maplydfz['type'] = "dianfeng_pay";
        $moneydfz  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplydfz)->sum('money');
        $this->assign('moneydfz', round($moneydfz,2));
        
        //昨日618
        $maply618z['shijian'] = ['like', "%" . $zuori . "%"];
        $maply618z['type'] = "618pay";
        $money618z  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maply618z)->sum('money');
        $this->assign('money618z', round($money618z,2));
                
        //昨日蘑菇
        $maplymgz['shijian'] = ['like', "%" . $zuori . "%"];
        $maplymgz['type'] = "mogupay";
        $moneymgz  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplymgz)->sum('money');
        $this->assign('moneymgz', round($moneymgz,2));
        
        //昨日冈本
        $maplygbz['shijian'] = ['like', "%" . $zuori . "%"];
        $maplygbz['type'] = "gangben_pay";
        $moneygbz  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplygbz)->sum('money');
        $this->assign('moneygbz', round($moneygbz,2));
        
        //昨日冈本金蝶
        $maplygbzjd['shijian'] = ['like', "%" . $zuori . "%"];
        $maplygbzjd['type'] = "gangen_jindie_pay";
        $moneygbjdz  = Db::name("dingdan")->where(['order_type'=>['<>',3]])->where($maplygbzjd)->sum('money');
        $this->assign('moneygbjdz', round($moneygbjdz,2));
        
        
        
        $this->assign('money3', round($money3, 2));
        $this->assign('wxddh', $wxddh);

        return $this->fetch();
    }
    
    
    
     public function zindex()
    {
        $key   = input('key');
        $start = input('start');
        $end   = input('end');
        $map = [];
        $ordermap = [];
        if ($key)
        {
            $map['o.userid'] = $key;
            
            $ordermap['userid'] = $key;
        }

        if ($start && $end)
        {
            
             $map['o.shijian'] = ['between',$start.','.$end];
             
             $ordermap['shijian'] = ['between',strtotime($start).','.strtotime($end)];
        }
        
        // if ($wxddh)
        // {
        //     $map['wxddh'] = $wxddh;
        // }
        // $map['order_type'] = ['<>',3];
        $order = new OrderModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 10; // 获取总条数
        $count = 100000; //计算总页面
        $allpage = intval(ceil($count / $limits));
      
        $lists = Db::name('order')->alias('o')->join('ip i', 'o.orderid=i.ddh2', 'LEFT')->join('member m', 'o.userid=m.id', 'LEFT')->where($map)->page($Nowpage, $limits)->order('o.id desc')->field('o.*,i.zt,m.nickname')->select();
        
        //当天开始时间
        $start_time=strtotime(date('Y-m-d').'00:00:00');
        //当天结束之间
        $end_time=strtotime(date('Y-m-d').'23:59:59');
        
        // var_dump(date("Y-m-d H:i:s",$start_time));
        // var_dump(date("Y-m-d H:i:s",$end_time));
        // die;
        
        //总order
        
        if($ordermap){
            $total_order = Db::name('order')->where($ordermap)->count();
            $Payment_order = Db::name('ip')->where($ordermap)->count();
        }else{
            $total_order = Db::name('order')->whereBetween('shijian',[$start_time,$end_time])->where($ordermap)->count();
            $Payment_order = Db::name('ip')->whereBetween('shijian',[$start_time,$end_time])->where($ordermap)->count();
            
        }
        

        $Payment_rate = sprintf("%.2f",substr(sprintf("%.3f", $Payment_order/$total_order*100), 0, -2));
        
        // var_dump($total_order);
        // var_dump($Payment_order);
        // var_dump($Payment_rate);
        // die;
        // $lists = $order->getOrderByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('Payment_rate', $Payment_rate);
        $this->assign('total_order', $total_order);
        $this->assign('Payment_order', $Payment_order);
        $this->assign('start', $start);
        $this->assign('end', $end);
        if (input('get.page')){
            foreach ($lists as &$v){
                $v['shijian'] = date('Y-m-d H:i:s',$v['shijian']);
                if(empty($v['zt'])){
                    $v['zt'] = "未支付"; 
                }
                $ip = $v['ip'];
                $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='".$ip."'&co=&resource_id=6006&t=1597758495883&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110204112591273233335_1597757755399&_=1597757755440";
                $arr = file_get_contents("compress.zlib://".$url);
                $content = iconv("GBK", "UTF-8", $arr);
                $result1 = strstr($content,"titlecont",1);
                $result2 = strstr($result1 ,"location");
                $pieces = explode(" ", $result2);
                $pieces1 = explode(",", $pieces[1]);
                $result3 = explode(":", $pieces[0].$pieces1[0].$pieces1[1]);
                $result3 = $result3[1].$result3[2];
                $v['ip_name'] = $result3;
                // $v['ip_name'] = "中国";
            }
            unset($v);
            return json($lists);
        }
        return $this->fetch();
    }

    
    public function search()
    {
        $key   = input('key');
        $wxddh = input('wxddh');
        $start = input('start');
        $end = input('end');
        $map = [];
        $map2 = "1";

        if ($start)
        {
            $map2 .= " and UNIX_TIMESTAMP(shijian)>=" . strtotime($start);
        }

        if ($end)
        {
            $map2 .= " and UNIX_TIMESTAMP(shijian)<" . strtotime($end);
        }
        if (empty($start) || empty($end))
        {
            $this->error("请选择时间");
        }
        if ($key)
        {
            $map['userid'] = $key;
        }
        if ($wxddh)
        {
            $map['wxddh'] = ['like', "%" . $wxddh . "%"];
        }
        
        $order = new OrderModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 10; // 获取总条数
        
        $count = $order->getAllCount2($map, $map2); //计算总页面
        $allpage = intval(ceil($count / $limits));

        $lists = $order->getOrderByWhere2($map, $map2, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
         $this->assign('wxddh', $wxddh);
        $this->assign('start', $start);
        $this->assign('end', $end);
        if (input('get.page'))
        {
            return json($lists);
        }

        if (!empty($key))
        {
            $map['userid'] = $key;
        }
        $map['order_type'] = ['<>',3];
        $daydd = Db::name("dingdan")->where($map)->where($map2)->where(['order_type'=>['<>',3]])->count();
        $this->assign('daydd', $daydd);

        $money1 = Db::name("dingdan")->where($map)->where($map2)->where(['order_type'=>['<>',3]])->sum('money');
        $this->assign('money1', round($money1, 2));

        return $this->fetch();
    }

    public function del_date_yqm()
    {
        $start = input('start');
        $end = input('end');
        if (empty($start) || empty($end))
        {
            $this->error("请选择时间");
        }
        $map2 = '1';
        if ($start)
        {
            $map2 .= " and UNIX_TIMESTAMP(shijian)>=" . strtotime($start);
        }

        if ($end)
        {
            $map2 .= " and UNIX_TIMESTAMP(shijian)<" . strtotime($end);
        } 
        
        $map2 .= " and order_type != 3";
        // return $map2;
        $Order = new OrderModel();
        $flag = $Order->del_date_yqm($map2);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
     public function del_member()
    {
        $id = input('param.id');
      
        $order = Db::name('dingdan')->where(['id' => $id])->find();
        if(!$order){
            die('订单不存在');
            
        }
        
        $info = Db::name('order')->where(['orderid' => $order['ddh']])->find();
      
        
       $openid = Db::name('ip')->where(['ddh2' => $order['ddh']])->value('openid');
         $shijian = date('Y-m-d H:i:s' , time());
        
       Db::name('ts')->insert(['ip' => $info['ip'],'uopenid' => $info['uopenid'],'zt' => "禁止访问",'neirong' => '内容投诉商户','shijian' =>$shijian,'openid' => $openid,'type' => 1]);
        
        $uopenid = Db::name('uopenid')->where(['uopenid' => $info['uopenid']])->find();
        
        if(!$uopenid){
            
           Db::name('uopenid')->insert(['uopenid' => $info['uopenid'],'time' => time(),'userid' => $info['userid']]);
            
        }
      
    }
    
    
     public function del_dwz()
    {
        $id = input('param.id');
      
        $order = Db::name('dingdan')->where(['id' => $id])->find();
        if(!$order){
            die('订单不存在');
            
        }
        
        $info = Db::name('ip')->where(['ddh2' => $order['ddh']])->find();
        
        
        $dwz  = Db::name('dwz')->where(['ddh' => $info['ddh']])->find();
        
        if($dwz){
            $flag  = Db::name('dwz')->where('id', $dwz['id'])->setField(['status' => 0]);
            $infos = Db::name('dwz')->where(['id' => $dwz['id']])->find();
            
            Cache::store('redis')->set($infos['jiami'],json_encode($infos));
            
        }
        
    }
    
    
    
    
}
